package servlet;
import com.google.gson.Gson;
import simulator.engine.Simulator;
import db.DatabaseKobling;
import java.io.FileWriter;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 500209
 *
 */
public class Submit extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
         PrintWriter out = response.getWriter();
        try {
            String profil = request.getParameter("structure");
            String beboere = request.getParameter("beboere");
            String startdato = request.getParameter("startDate");
            String sluttdato = request.getParameter("stopDate");
            
            int startMM =Integer.parseInt(startdato.split("-")[1]);
            int startDD =Integer.parseInt(startdato.split("-")[2]);
            int startYY = Integer.parseInt(startdato.split("-")[0].substring(2));
            int startHH = 1; //Integer.parseInt(startdato.split("-")[3]);
            
            int sluttMM =Integer.parseInt(sluttdato.split("-")[1]);
            int sluttDD =Integer.parseInt(sluttdato.split("-")[2]);
            int sluttYY = Integer.parseInt(sluttdato.split("-")[0].substring(2));
            int sluttHH = 1; //Integer.parseInt(startdato.split("-")[3]);
            
            startdato = startDD + "," + startMM + "," + startYY + "," + startHH;
            sluttdato = sluttDD + "," + sluttMM + "," + sluttYY + "," + sluttHH;
            
            
            String kommune = request.getParameter("kommune");
            String fylke = request.getParameter("fylke");
            String station = request.getParameter("station");
            String tidsfrekvens = request.getParameter("tidsfrekvens");
            String[] energiForbrukLight = request.getParameterValues("energiForbrukLight[]");
            float[] light = new float[energiForbrukLight.length];
            for(int i = 0; i < energiForbrukLight.length; i++) {
                light[i] = Float.parseFloat(energiForbrukLight[i])/100.0f;      
            }
            String[] energiForbrukEquip = request.getParameterValues("energiForbrukEquip[]");
            String[] utetemp = request.getParameterValues("utetemp[]");
            String[] innetemp = request.getParameterValues("innetemp[]");
            int waterTankTemp = Integer.valueOf(request.getParameter("waterTemp"));
            int waterUsing = Integer.valueOf(request.getParameter("waterUsing"));
            int population = Integer.valueOf(request.getParameter("personCount"));
            
            float hoyde = Float.valueOf(request.getParameter("vegghoyde"));
            float bredde = Float.valueOf(request.getParameter("veggbredde"));
            float uverdi = Float.valueOf(request.getParameter("uverdi"));

            int inhabitantsId = DatabaseKobling.getInhabitantsId(beboere);
            int STRUCTURE_ID = DatabaseKobling.getStructureId(profil.trim());
            int numberOfLights = DatabaseKobling.getNumberofLights(STRUCTURE_ID);
            float[][] walls = new float[4][3];
            
            for(int i = 0; i < walls.length; i++)
            {
                walls[i][0] = hoyde;
                walls[i][1] = bredde;
                walls[i][2] = uverdi;
            }
            
            Simulator sim = new Simulator();
            Gson gson = new Gson();

            //response.getWriter().write(new Gson().toJson(yo));
            //response.getOutputStream().print(gson.toJson(yo));
	    //response.getOutputStream().flush();
            //sim.simulate(inhabitantID, inhabitantsId, inhabitantsId, inhabitantsId, inhabitantsId, inhabitantsId, inhabitantsId, userPatternForIlumination, test, kommune, walls, inhabitantsId);
            //FileWriter fw = new FileWriter(XML_FILE_NAME);
            //simulator.engine.Simulator.setWaterHeater(population, consumtion, insideTemprature);
            //simulator.engine.Simulator.setIllumination(numberOfLights, kilowatts);
            //simulator.engine.Simulator.setInventory(inhabitantsId, structureID);
            
            String simulation = gson.toJson(sim.simulate(inhabitantsId, STRUCTURE_ID, waterUsing, 
                    population, waterTankTemp, numberOfLights, 60, light, startdato, sluttdato, walls, 2000, fylke, station));
            out.print(simulation);
            out.flush();
            out.close();
            //request.setAttribute("data", yo);
            //request.getRequestDispatcher("chart.jsp").forward(request, response); 
            
            //out.println(usagepattern);
            
        } catch(Exception e) {            
            e.printStackTrace();;
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    
}
